package com.secsbrain.development.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.client.iface.AppInfoService;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.development.biz.IBranchService;
import com.secsbrain.development.biz.IProjectService;
import com.secsbrain.development.biz.model.ProjectParam;
import com.secsbrain.development.dal.model.BranchTypeRule;
import com.secsbrain.development.dal.model.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import springfox.documentation.annotations.ApiIgnore;

@Controller
@RequestMapping("api/prj")
@ApiIgnore
public class ProjectController {
	
    private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);
    
    /**需要申请机器的环境类型*/
    public static final String SPECIAL_ENV="dev";
    
    @Autowired
    private IBranchService branchService;
    
    @Autowired
    private IProjectService projectService;
    
    @Autowired
    AppInfoService appInfoService;
    
//    @RequestMapping("list")
//    @ResponseBody
//    public List<App> list(Integer start, Integer limit, BranchActionRecord branchActionRecord){
//        //TODO 所有应用相关的，需要筛选出未删除的
//        return appInfoService.queryAllAppList().getData();
//    }

    /**
     * 功能描述: <br>
     * 查询分支类型
     *
     * @return
     */
    @RequestMapping("type")
    @ResponseBody
    public ResponseResult<List<BranchTypeRule>> type(){
        Map<Long, BranchTypeRule> map=branchService.getBranchTypeCache();
        
        List<BranchTypeRule> list=new ArrayList<>();
        Set<Map.Entry<Long, BranchTypeRule>> entryseSet=map.entrySet();
        for (Map.Entry<Long, BranchTypeRule> entry:entryseSet) {
            if(SPECIAL_ENV.equals(entry.getValue().getEnvType())){
                list.add(entry.getValue());
            }
        }
        return new ResponseResult<>(list);
    }
    
    /**
     * 功能描述: <br>
     * 创建项目
     *
     * @return
     */
    @RequestMapping("create")
    @ResponseBody
    public ResponseResult create(ProjectParam projectParam, HttpSession session) {
    	//TODO 获取当前用户信息
//        UserInfoDTO userInfo = (UserInfoDTO) session.getAttribute("userInfo");
//        projectParam.setCreateUser(userInfo.getName());
//        projectParam.setCreateId(userInfo.getId());

        ResponseResult baseResult=projectService.createProject(projectParam);
        logger.debug("立项结果：{}",baseResult);
        return baseResult;
    }
    
    /**
     * 功能描述: <br>
     * 修改项目
     *
     * @return
     */
    @RequestMapping("modify")
    @ResponseBody
    public ResponseResult modify(ProjectParam projectParam,HttpSession session) {
      //TODO 获取当前用户信息
//      UserInfoDTO userInfo = (UserInfoDTO) session.getAttribute("userInfo");
//      projectParam.setCreateUser(userInfo.getName());
//      projectParam.setCreateId(userInfo.getId());

        ResponseResult baseResult=projectService.modifyProject(projectParam);
        logger.debug("修改项目结果：{}",baseResult);
        return baseResult;
    }
    
    /**
     * 功能描述: <br>
     * 查询项目记录
     *
     * @param start
     * @param limit
     * @param record
     * @return
     */
    @RequestMapping("recordList")
    @ResponseBody
    public ResponseResult<Page<Record>> recordList(Integer start, Integer limit, Record record){
        return projectService.queryPrjList(start, limit, record);
    }
    
    /**
     * 功能描述: <br>
     * 结项
     *
     * @return
     */
    @RequestMapping("over")
    @ResponseBody
    public ResponseResult over(ProjectParam projectParam,HttpSession session) {
        ResponseResult baseResult=projectService.overProject(projectParam);
        logger.debug("结项结果：{}",baseResult);
        return baseResult;
    }
    
    
}
